<template>
  <uni-forms :model-value="addForm" :rules="formRules" ref="locationRef">
    <view class="select-location">
      <view class="data-select">
        <uni-forms-item name="location">
          <uni-data-picker v-model="addForm.location" placeholder="请选择存放仓库" :localdata="locationOp"></uni-data-picker>
        </uni-forms-item>
      </view>
    </view>
  </uni-forms>
</template>
<script setup lang="ts" name="">
const addForm = reactive({
  location: ""
});
const formRules = {
  location: {
    rules: [
      {
        required: true,
        errorMessage: "存放位置不能为空"
      }
    ]
  }
};

const locationRef = ref();

const locationOp: any = ref([]);

// 设置存放位置选项内容
const setLocationOp = async () => {
  locationOp.value = await locationEnableTree();
};

onBeforeMount(() => {
  setLocationOp();
});

// 表单验证
const validateForm = (): Promise<string> => {
  return new Promise<string>((resolve, reject) => {
    locationRef.value
      .validate()
      .then(() => {
        resolve(addForm.location);
      })
      .catch(err => {
        reject(err);
      });
  });
};

defineExpose({
  validateForm
});
</script>
<style lang="scss" scoped></style>
